package com.ruoyi.system.service.project.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.project.teachpaper.TeachpaperInfo;
import com.ruoyi.system.domain.project.teachpaper.TeachpaperInfoTeacher;
import com.ruoyi.system.mapper.project.TeachpaperInfoMapper;
import com.ruoyi.system.mapper.project.TeachpaperInfoTeacherMapper;
import com.ruoyi.system.service.project.ITeachpaperInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 教改论文信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-09
 */
@Service
public class TeachpaperInfoServiceImpl implements ITeachpaperInfoService
{
    @Autowired
    private TeachpaperInfoMapper teachpaperInfoMapper;
    @Autowired
    private TeachpaperInfoTeacherMapper teachpaperInfoTeacherMapper;

    /**
     * 查询教改论文信息
     * 
     * @param id 教改论文信息主键
     * @return 教改论文信息
     */
    @Override
    public TeachpaperInfo selectTeachpaperInfoById(Long id)
    {
        TeachpaperInfo paperInfo = teachpaperInfoMapper.selectPaperInfoById(id);
        TeachpaperInfoTeacher teacher = new TeachpaperInfoTeacher();
        paperInfo.setId(id);
        List<TeachpaperInfoTeacher> teachers = teachpaperInfoTeacherMapper.selectTeachpaperInfoTeacherList(teacher);
        paperInfo.setTeachers(teachers);
        return paperInfo;
    }

    /**
     * 查询教改论文信息列表
     * 
     * @param teachpaperInfo 教改论文信息
     * @return 教改论文信息
     */
    @Override
    public List<TeachpaperInfo> selectTeachpaperInfoList(TeachpaperInfo teachpaperInfo)
    {
        //如果是老师，则只显示自己上传的
        if(teachpaperInfo.getCurrentRole()!=null&&teachpaperInfo.getCurrentRole().equals("teacher")){
            teachpaperInfo.setAuthorId( getUserId() );
        }
        //如果是部门，则显示老师是系里的所有
        if(teachpaperInfo.getCurrentRole()!=null&&teachpaperInfo.getCurrentRole().equals("department")){
            teachpaperInfo.setDeptId(SecurityUtils.getDeptId());
        }
        List<TeachpaperInfo> paperInfos = teachpaperInfoMapper.selectPaperInfoList(teachpaperInfo);
        for(TeachpaperInfo ra : paperInfos){
            TeachpaperInfoTeacher teacher = new TeachpaperInfoTeacher();
            teacher.setPaperId(ra.getId());
            List<TeachpaperInfoTeacher> teachers = teachpaperInfoTeacherMapper.selectTeachpaperInfoTeacherList(teacher);
            ra.setTeachers(teachers);
        }
        return paperInfos;
    }

    /**
     * 新增教改论文信息
     * 
     * @param teachpaperInfo 教改论文信息
     * @return 结果
     */
    @Override
    public int insertTeachpaperInfo(TeachpaperInfo teachpaperInfo)
    {
        if(teachpaperInfo.getId()!=null){
            TeachpaperInfoTeacher teacher = new TeachpaperInfoTeacher();
            teacher.setPaperId(teachpaperInfo.getId());
            teacher.setRank((long) teachpaperInfo.getRank());
            teacher.setUserId( teachpaperInfo.getAuthorId());
            teacher.setUserType(String.valueOf(teachpaperInfo.getAuthorType()));
            teacher.setUserName(teachpaperInfo.getAuthorName());
            teachpaperInfoTeacherMapper.insertTeachpaperInfoTeacher( teacher);
        }else {
            int result = teachpaperInfoMapper.insertPaperInfo(teachpaperInfo);
            if(result>0){
                TeachpaperInfoTeacher teacher = new TeachpaperInfoTeacher();
                teacher.setPaperId(teachpaperInfo.getId());
                teacher.setRank((long) teachpaperInfo.getRank());
                teacher.setUserId( teachpaperInfo.getAuthorId());
                teacher.setUserType(String.valueOf(teachpaperInfo.getAuthorType()));
                teacher.setUserName(teachpaperInfo.getAuthorName());
                teachpaperInfoTeacherMapper.insertTeachpaperInfoTeacher( teacher);
            }
        }
        return 1;
    }

    /**
     * 修改教改论文信息
     * 
     * @param teachpaperInfo 教改论文信息
     * @return 结果
     */
    @Override
    public int updateTeachpaperInfo(TeachpaperInfo teachpaperInfo)
    {
        return teachpaperInfoMapper.updatePaperInfo(teachpaperInfo);
    }

    /**
     * 批量删除教改论文信息
     * 
     * @param ids 需要删除的教改论文信息主键
     * @return 结果
     */
    @Override
    public int deleteTeachpaperInfoByIds(Long[] ids)
    {
        for(Long id : ids){
            TeachpaperInfoTeacher teacher = new TeachpaperInfoTeacher();
            teacher.setPaperId(id);
            List<TeachpaperInfoTeacher> teachers = teachpaperInfoTeacherMapper.selectTeachpaperInfoTeacherList(teacher);
            if(!teachers.isEmpty()){
                for (TeachpaperInfoTeacher paperInfoTeacher : teachers) {
                    teachpaperInfoTeacherMapper.deleteTeachpaperInfoTeacherById(paperInfoTeacher.getId());
                }
            }
        }
        return teachpaperInfoMapper.deletePaperInfoByIds(ids);
    }

    /**
     * 删除教改论文信息信息
     * 
     * @param id 教改论文信息主键
     * @return 结果
     */
    @Override
    public int deleteTeachpaperInfoById(Long id)
    {
        return teachpaperInfoMapper.deletePaperInfoById(id);
    }
    /**
     * 根据标题查询论文信息
     * @param data
     * @return
     */
    @Override
    public TeachpaperInfo getPaperInfoByTitle(String data) {
        return teachpaperInfoMapper.getPaperInfoByTitle(data);
    }


}
